package _23define;

import java.io.*;

public class Demo4 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //serlalize();
        unSerlalize();

    }

    private static void unSerlalize() throws IOException, ClassNotFoundException {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("src/note/b.txt"));
        Object o=in.readObject();
        System.out.println(o);
        in.close();
    }

    private static void serlalize() throws IOException {
        Address address = new Address("详细地址", "邮政编码");
        User user = new User(1, "张三", "1234567890", address);

        // 序列化并保存到文件
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("src/note/b.txt"));
        out.writeObject(user);
        out.close();
    }
}
class User implements Serializable{
    int id;
    String name;
    String mobile;
    Address address;


    public User(int id, String name, String mobile, Address address) {
        this.id = id;
        this.name = name;
        this.mobile = mobile;
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", mobile='" + mobile + '\'' +
                ", address=" + address +
                '}';
    }
}
class Address implements Serializable {
    String detail;
    String code;

    public Address(String detail, String code) {
        this.detail = detail;
        this.code = code;
    }


    @Override
    public String toString() {
        return "Address{" +
                "detail='" + detail + '\'' +
                ", code='" + code + '\'' +
                '}';
    }
}
